#include "../../Prec.h"

USING_NS_CC;

bool CCGameScene::init()
{
  if(!CCLayer::init())
    return false;
  CCSize size = CCDirector::sharedDirector()->getWinSize();

  mWorld = new CBWorld(0,-30);
  mWorld->mWidth = 40;
  mWorld->mHeight = 60;

  mWorld->mCBworld2CocoProportionH = size.width/mWorld->mWidth;
  mWorld->mCBworld2CocoProportionV = size.height/mWorld->mHeight;

  CBSide* topside = new CBSide(mWorld
    ,ccp(0,mWorld->mHeight)
    ,ccp(mWorld->mWidth,mWorld->mHeight)
    ,EST_TOP);
  mWorld->mBodyLst.push_front(topside);

  CBSide* leftside = new CBSide(mWorld
    ,ccp(0,0)
    ,ccp(0,mWorld->mHeight)
    ,EST_LEFT);
  mWorld->mBodyLst.push_front(leftside);

  CBSide* rightside = new CBSide(mWorld
    ,ccp(mWorld->mWidth,0)
    ,ccp(mWorld->mWidth,mWorld->mHeight)
    ,EST_RIGHT);
  mWorld->mBodyLst.push_front(rightside);

  int randindex = rand()%4;
  mNextBall = CCBall::BallWithFile("paopaolong.png",CCRect(randindex*32,0,32,32));
  mNextBall->setScaleX(size.width/320);
  mNextBall->setScaleY(size.height/480);
  mNextBall->setPosition(ccp(size.width/2,mNextBall->getContentSize().height/2));
  CBBall* ball = new CBBall(mWorld,mWorld->coco2cbworldH(mNextBall->getPosition().x),mWorld->coco2cbworldV(mNextBall->getPosition().y));
  ball->mColor = randindex;
  mNextBall->mBall = ball;
  mNextBall->mBall->mShow = mNextBall;

  mDir = CCNode::create();
  CCSprite* mDirSprite = CCSprite::spriteWithFile("paopaolong.png",CCRect(128,0,64,64));
  mDir->setScaleX(size.width/320);
  mDir->setScaleY(size.height/480);
  mDirSprite->setPosition(ccp(0,mDirSprite->getContentSize().height/2));
  mDir->addChild(mDirSprite);
  mDir->setPosition(ccp(size.width/2,mNextBall->getContentSize().height/2));
  mDir->setVisible(true);

  this->addChild(mDir);
  this->addChild(mNextBall);

  

  this->scheduleUpdate();
  this->setTouchEnabled(true);
  return true;
}

CCScene* CCGameScene::scene()
{
  // 'scene' is an autorelease object
  CCScene *scene = CCScene::create();

  // 'layer' is an autorelease object
  CCGameScene *layer = CCGameScene::create();

  // add layer as a child to scene
  scene->addChild(layer);

  // return the scene
  return scene;
}

void CCGameScene::update(float dt)
{
  std::list<CCSprite*> removelst;
  mWorld->Tick(dt,removelst);

  for(std::list<CBBody*>::iterator iter = mWorld->mBodyLst.begin();iter!=mWorld->mBodyLst.end();)
  {
    CBBody* body = *iter;
    if(body->mShow)
    {
      if(body->mType==EBT_Ball)
      {
        CBBall* ball = (CBBall*)body;
        ball->mShow->setPosition(ccp(mWorld->cbworld2cocoH(ball->mCircle.x),mWorld->cbworld2cocoV(ball->mCircle.y)));
        if(ball->mShow->getPosition().y<-32)
        {
          if(removelst.size()>0)
          {
            for(std::list<CCSprite*>::iterator subiter = removelst.begin();subiter!=removelst.end();)
            {
              if(*subiter==ball->mShow)
              {
                subiter = removelst.erase(subiter);
              }
              else 
                ++subiter;
            }
          }
          
          iter = mWorld->mBodyLst.erase(iter);
          this->removeChild(ball->mShow,true);
          continue;
        }
      }
    }
    ++iter;
  }
  if(removelst.size()>=3)
  {
    for(std::list<CCSprite*>::iterator iter = removelst.begin();iter!=removelst.end();++iter)
    {
      this->removeChild(*iter,true);
    }
  }
  else if(mWorld->mbLose)
  {
    exit(0);
  }
}

void CCGameScene::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
  std::list<CBBody*>::iterator iterbody = mWorld->mBodyLst.begin(); 
  if(iterbody!=mWorld->mBodyLst.end()&& (*iterbody)->mType==EBT_Ball)
  {
    CBBall* ball = (CBBall*)*iterbody;
    if(ball->mForce==false)
      return ;
  }
  
  CCSize size = CCDirector::sharedDirector()->getWinSize();
  
  CCSetIterator iter = pTouches->begin();
  CCTouch* touch = (CCTouch*)*iter;
  
  CCPoint pt = CCDirector::sharedDirector()->convertToGL(touch->locationInView());
  float deltax = pt.x-size.width/2;
  float deltay = pt.y;

  float deg = 90-atan2f(deltay,deltax)*180/M_PI;
  mDir->setRotation(deg);

  mWorld->mBodyLst.push_front(mNextBall->mBall);
  float dis = CCDefine::Instance()->Distance(deltax,deltay);
  mNextBall->mBall->mSpeed.x = CCDefine::Instance()->BallSpeed()*deltax/dis;
  mNextBall->mBall->mSpeed.y = CCDefine::Instance()->BallSpeed()*deltay/dis;
  
  int randindex = rand()%4;
  mNextBall = CCBall::BallWithFile("paopaolong.png",CCRect(randindex*32,0,32,32));
  mNextBall->setScaleX(size.width/320);
  mNextBall->setScaleY(size.height/480);
  mNextBall->setPosition(ccp(size.width/2,mNextBall->getContentSize().height/2));
  CBBall* ball = new CBBall(mWorld,mWorld->coco2cbworldH(mNextBall->getPosition().x),mWorld->coco2cbworldV(mNextBall->getPosition().y));
  ball->mColor = randindex;
  mNextBall->mBall = ball;
  mNextBall->mBall->mShow = mNextBall;
  
  this->addChild(mNextBall);
}
